# -*- coding: utf-8 -*-

import os
import string

import time
from datetime import datetime, timedelta
import hashlib
import json
import ssl

import jpype
from jpype import *
import os.path


def main():
    jvmPath = getDefaultJVMPath()

    source_dir = os.getcwd()
    #jarpath = source_dir + '/libs/NBIot.jar'
    jarpath = source_dir + "/libs/NBIot.jar:" +\
            source_dir + "/libs/commons-codec-1.9.jar:" +\
            source_dir + "/libs/commons-logging-1.2.jar:" +\
            source_dir + "/libs/httpclient-4.5.2.jar:" + \
            source_dir + "/libs/httpcore-4.4.4.jar:" + \
            source_dir + "/libs/httpmime-4.5.2.jar:" + \
            source_dir + "/libs/jackson-annotations-2.5.4.jar:" + \
            source_dir + "/libs/jackson-core-2.5.4.jar:" + \
            source_dir + "/libs/jackson-databind-2.5.5.jar"

    #jvmPath = 'C:/Program Files/Java/jre1.8.0_151/bin/server/jvm.dll'
    print jvmPath
    print jarpath

    #jvmargs = ["-Djava.class.path=./lib/jmotif.lib-0.97.jar:./lib/hackystatlogger.lib.jar:\
    #./lib/hackystatuserhome.lib.jar:./lib/weka.jar",
    #    "-Djava.library.path=./lib/rjava/jri"]
    #startJVM(jpype.getDefaultJVMPath(), *jvmargs)
    startJVM(jvmPath, "-Djava.class.path=%s" % jarpath)
    AppTaskClass = JClass("NBIot.AppTask")
    #NBIot = JPackage('NBIot')

    serverdata = {
        "IP": "180.101.147.89",
        "Port": "8743",
        "AppId": "RYjXfz9XR66754XiIRn4SGDGakUa",
        "Password": "tR1lThRZpaG7j6LSfZqIqnC04qUa"
    }

    AppTask = AppTaskClass(serverdata['IP'], serverdata['Port'], serverdata['AppId'], serverdata['Password'])

    bRet = AppTask.authentication()
    #Authentication
    if bRet == False:
        print "Auth Failed, Check AppID and Password."
        return
    print "Authentication successed!."

    strDeviceId = '21e765fa-b696-40e0-aa4c-cd768b7d7679'

    historydata = AppTask.queryRawData(strDeviceId)
    print(type(historydata))
    print(historydata)

    commandData = 'MDAwMDAwMDAwMzAwMDA0NjQ2'
    #'000000000300004646'
    #callbackUrl = 'https://rgwl.xyz:8080/rest/commandRsp/subscriber'

    #AppTask.subscribe('commandRsp', callbackUrl)
    historydata = AppTask.postAsyncCommand(strDeviceId, 'CommandSend', 'CommandSend', 'rawData', commandData, '0')
    print(historydata)


if __name__ == "__main__":
    main()